<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
  >
    <template #searchPanel>
      <search-panel
        :addPer="'storageSite:save'"
        :selectPer="'storageSite:page'"
        addTitle="新增仓位"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <Job-shop v-model="form.storeId" placeholder="所属门店" />
        <el-select
          clearable
          style="width: 200px"
          v-model="form.storageId"
          placeholder="仓库"
        >
          <el-option
            v-for="item in storageList"
            :key="item.id"
            :value="item.id"
            :label="item.storageName"
          />
        </el-select>
        <el-select
          clearable
          style="width: 200px"
          v-model="form.storageSiteType"
          placeholder="仓位类别"
        >
          <el-option
            v-for="item in dict['仓位类别']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-input
          v-model="form.likeName"
          clearable
          placeholder="仓位编号/仓位名称"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
        @row-dblclick="view"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="cursor: pointer"
            v-permission="'storageSite:update'"
            @click="handleDataUpdateEdit(row, '编辑')"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'storageSite:del'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
    <template #dialog>
      <dialog-view-detail
        @operate="handleDataOperate"
        :id="viewId"
        @oncancel="dialogClose"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from "@/mixins/base";
import { getDict } from "@/utils";
import { page, deleteById } from "@/api/after-sale/storehouseManage/positionManage";
import { findByStore } from "@/api/after-sale/storehouseManage/warehouseManage";
import DialogViewDetail from "../view/index.vue";
export default {
  components: {
    DialogViewDetail,
  },
  mixins: [
    baseMixin({
      idName: "id",
      findData: page,
      dataName: ["content", "totalRecords"],
      format(res) {
        return res.map((data) => {
          data.preferred = data.preferred == true ? "是" : "否";
          data.openRestsStoreName = data.openRestsStore == true ? "启用" : "禁用";
          return data;
        });
      },
      deleteData: deleteById,
    }),
  ],
  data() {
    return {
      tableColumnAttributes: [
        { name: "所属门店", value: "storeName", type: "text" },
        { name: "仓位编号", value: "storageSiteCode", type: "text" },
        { name: "仓位名称", value: "storageSiteName", type: "text" },
        { name: "仓位类别", value: "storageSiteType", type: "text" },
        { name: "所属仓库", value: "storageName", type: "text" },
        { name: "存放配件", value: "partsName", type: "text" },
        { name: "可存放数量", value: "num", type: "text" },
        { name: "已存放数量", value: "alreadyNum", type: "text" },
        { name: "剩余存放数量", value: "remainNum", type: "text" },
        { name: "最后盘点时间", value: "checkDate", type: "text" },
        { name: "状态", value: "status", type: "text" },
        { name: "仓位管理人", value: "userName", type: "text" },
        { name: "管理人联系方式", value: "phone", type: "text" },
        { name: "仓位备注", value: "remark", type: "text" },
        { name: "录入时间", value: "createTime", type: "text" },
      ],
      dict: [],
      dialogVisible: false,
      storageList: [],
      form: {
        storeId: "",
      },
      viewId: "", // 查看id
    };
  },
  watch: {
    "form.storeId": function (value) {
      if (value) {
        this.getWareHouse(value);
      }
    },
  },
  methods: {
    add() {
      this.$router.push({ path: "/storehouseManage/addPositionManage" });
    },
    handleDataUpdateEdit(row, type) {
      this.$router.push({
        path: "/storehouseManage/editPositionManage",
        query: {
          id: row.id,
          alreadyNum: row.alreadyNum,
        },
      });
    },
    search(form) {
      this.searchOptions = this.form;
      this.currentPage = 1;
      this.setTableData();
    },
    // 获取仓库
    async getWareHouse(id) {
      let { data } = await findByStore(id);
      this.storageList = data;
    },
    // 查看仓库
    view(row) {
      this.viewId = row.id;
      this.handleDataInsert("仓位详情");
    },
  },
  mounted() {
    this.dict = getDict(["仓位类别"]);
  },
};
</script>
